﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

namespace ORT
{
    public class TestCircle : MonoBehaviour
    {
        public Image testCircleUI;
        public Transform thisTransform;
  
      
        public Text IDTxt;
        public Vector2 circlePos = new Vector2();

        public Vector3 fangxiang = Vector3.zero;
        public float m_Radius;
        public Transform[] points;
        public Vector3 preAngles = Vector3.zero;
        public List<Vector2> prePoints = new List<Vector2>();

        public CircleData circleData;

        private Vector3 preFirstPoint = Vector3.zero;
 

        private bool hasNo = true;

#if UNITY_EDITOR
        public float m_Theta = 0.1f; // 值越低圆环越平滑
        public Color m_Color = Color.green; // 线框颜色
#endif
        // Use this for initialization
        void Start()
        {
            if (thisTransform == null)
            {
                throw new Exception("Transform is NULL.");
            }

            if (!ObjectRecognitionTable.checkTouch)
            {
                testCircleUI.color = new Color(255, 255, 255, 0);
                IDTxt.color = new Color(0, 0, 0, 0);
            }
            else
            {
                testCircleUI.color = new Color(255, 255, 255, 1);
                IDTxt.color = new Color(0, 0, 0, 1);
            }
        }

        public float curAngle = 0;
        public void SetPoints(List<Vector2> list, Vector3 angles, bool isFirst)
        {
            for (int i = 0; i < 3; i++)
            {
                points[i].gameObject.SetActive(true);
                points[i].localPosition = new Vector3(list[i].x - transform.localPosition.x, list[i].y - transform.localPosition.y, 0);
                //if (isFirst)
                {
                    //points[i + 3].gameObject.SetActive(true);
                    //points[i + 3].localPosition = new Vector3(list[i].x - transform.localPosition.x, list[i].y - transform.localPosition.y, 0);
                }
            }
            //Debug.Log(points[0].position.ToString() + "   " + points[1].position + "    " + points[2].position);
            Vector3 specialAngle = GetSpecialAngle(angles);
            if (isFirst || hasNo)
            {
                curAngle = 0;
                testCircleUI.transform.localScale = Vector3.one * ObjectRecognitionTable.uiScale;
                fangxiang = specialAngle - testCircleUI.transform.position;
            }
            else
            {
                float angle = Vector3.Angle(fangxiang, specialAngle - testCircleUI.transform.position);
                fangxiang = specialAngle - testCircleUI.transform.position;
                curAngle += angle;
                testCircleUI.transform.localRotation = Quaternion.Euler(0, 0, curAngle);
                if (Recognition.instance.mRecognitionIDAngle != null)
                {
                    Recognition.instance.mRecognitionIDAngle(circleData.id, angle);
                }
                Debug.Log(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + " B=" + circleData.id + "  =当前旋转角度=" + angle);
            }
            preFirstPoint = specialAngle;

            //Debug.Log ("  =特殊角度=" + specialAngle);
        }

        Vector3 GetNearestPoint()
        {
            float distance = Vector3.Distance(preFirstPoint, points[0].transform.position);
            int idx = 0;
            for (int i = 1; i < 3; i++)
            {
                float distance1 = Vector3.Distance(preFirstPoint, points[i].transform.position);
                if (distance1 < distance)
                {
                    idx = i;
                }
            }
            return points[idx].transform.position;
        }

        Vector3 GetSpecialAngle(Vector3 angles)
        {
            if (Mathf.Abs(angles.x - circleData.specialAngle) < 10)
            {
                //Debug.Log (angles + "==================0==================" + circleData.specialAngle);
                return points[0].transform.position;
            }
            else if (Mathf.Abs(angles.y - circleData.specialAngle) < 10)
            {
                //Debug.Log (angles + "==================1==================" + circleData.specialAngle);
                return points[1].transform.position;
            }
            else
            {
                //Debug.Log (angles + "==================2==================" + circleData.specialAngle);
                return points[2].transform.position;
            }
        }

        public void SetID(CircleData data)
        {
            IDTxt.text = data.id.ToString();
            circleData = data;
        }

        private float curTime = 0;
        public void UpdateNoCheckTime(bool isShow)
        {
            if (!isShow && gameObject.activeSelf)
            {
                curTime += Time.deltaTime;
                if (curTime >= ObjectRecognitionTable.checkNoTime)
                {
                    gameObject.SetActive(false);
                    curTime = 0;
                    fangxiang = preFirstPoint - transform.position;
                    curAngle = 0;
                    hasNo = true;
                    if (Recognition.instance.mRecognitionID != null)
                    {
                        Recognition.instance.mRecognitionID(circleData.id, false);
                    }
                }
            }
            else
            {
                hasNo = false;
                curTime = 0;
            }
        }

        public void UIHideMe()
        {
            gameObject.SetActive(false);
            curTime = 0;
            fangxiang = preFirstPoint - transform.position;
            curAngle = 0;
            hasNo = true;
        }

        // Update is called once per frame
        void Update()
        {
        }

#if UNITY_EDITOR
        void OnDrawGizmos()
        {
            if (thisTransform == null) return;
            if (m_Theta < 0.0001f) m_Theta = 0.0001f;
            // 设置矩阵
            Matrix4x4 defaultMatrix = Gizmos.matrix;
            Gizmos.matrix = thisTransform.localToWorldMatrix;
            // 设置颜色
            Color defaultColor = Gizmos.color;
            Gizmos.color = m_Color;
            // 绘制圆环
            Vector3 beginPoint = Vector3.zero;
            Vector3 firstPoint = Vector3.zero;
            for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
            {
                float x = m_Radius * Mathf.Cos(theta);
                float z = m_Radius * Mathf.Sin(theta);
                Vector3 endPoint = new Vector3(x, z, 0);
                if (theta == 0)
                {
                    firstPoint = endPoint;
                }
                else
                {
                    Gizmos.DrawLine(beginPoint, endPoint);
                }
                beginPoint = endPoint;
            }
            // 绘制最后一条线段
            Gizmos.DrawLine(firstPoint, beginPoint);
            // 恢复默认颜色
            Gizmos.color = defaultColor;
            // 恢复默认矩阵
            Gizmos.matrix = defaultMatrix;
        }
#endif
    }
}

